Skip to contentSkip to conversation

Change in openvpn[master]: socket: let an optional UDP socket give up on a failed bind

Openvpn-devel Superseded stipa (Code Review) via Openvpn-devel 2 participants 18 messages Sep 14 – Nov 24 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1789395512000.I24e5925a72aaede53ef9e863fe96fcd7363df402@gerrit.openvpn.net

#1 lore raw

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email

to review the following change.

Change subject: socket: let an optional UDP socket give up on a failed bind
......................................................................

socket: let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process therefore kills a client that would have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 29 insertions(+), 14 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/1

src/openvpn/manage.c +1 -1
index e121b38..46b0abe 100644
@@ -2044,7 +2044,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +16 -8
index 41c8bac..4c0aab5 100644
@@ -627,12 +627,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -678,9 +679,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -914,9 +917,9 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix,
-            bool ipv6only)
+            bool ipv6only, msglvl_t msglevel)
 {
     struct gc_arena gc = gc_new();
 
@@ -940,8 +943,10 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        gc_free(&gc);
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -956,10 +961,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
             print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
     gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 46fa29c..20c10c2 100644
@@ -348,8 +348,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
-                 bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
+                 bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -405,9 +412,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 1
Gerrit-Owner: stipa <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email

to look at the new patch set (#2).

Change subject: socket: let an optional UDP socket give up on a failed bind
......................................................................

socket: let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process therefore kills a client that would have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 32 insertions(+), 17 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/2

src/openvpn/manage.c +1 -1
index e121b38..46b0abe 100644
@@ -2044,7 +2044,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +16 -8
index 41c8bac..4c0aab5 100644
@@ -627,12 +627,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -678,9 +679,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -914,9 +917,9 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix,
-            bool ipv6only)
+            bool ipv6only, msglvl_t msglevel)
 {
     struct gc_arena gc = gc_new();
 
@@ -940,8 +943,10 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        gc_free(&gc);
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -956,10 +961,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
             print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
     gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +15 -8
index 46fa29c..4db3f3b 100644
@@ -348,8 +348,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
-                 bool ipv6only);
+/**
+ * Bind \p sd to the first \p af_family address of \p local. A failure is
+ * reported at \p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
+                 bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -402,12 +409,12 @@
 socket_descriptor_t create_socket_tcp(struct addrinfo *);
 
 /**
- * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
- * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
- * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * Create a UDP socket for \p af set up the way a link socket is: --sndbuf/--rcvbuf,
+ * --mark, --bind-dev and, when \p bind_addr is given, the local bind (IPV6_V6ONLY
+ * per \p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
+ * so a probe socket is the connection socket it may become. With \p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 2
Gerrit-Owner: stipa <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos, stipa.

ralf_lici has posted comments on this change by stipa. ( http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

Change subject: socket: let an optional UDP socket give up on a failed bind
......................................................................

Patch Set 7: Code-Review+1

(1 comment)

File src/openvpn/socket.c:

http://gerrit.openvpn.net/c/openvpn/+/1915/comment/606cdaa3_11d58094?usp=email :
PS7, Line 965: print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
nit: Should this now print cur->ai_addr rather than local->ai_addr? local may point to an entry of another address family, while cur is the address on which the failed bind was actually attempted.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 7
Gerrit-Owner: stipa <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-Reviewer: ralf_lici <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: stipa <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>
Gerrit-Comment-Date: Thu, 24 Sep 2026 12:38:28 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#4 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: plaisthos, ralf_lici, stipa.

Hello plaisthos, ralf_lici,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email

to look at the new patch set (#8).

The following approvals got outdated and were removed:
Code-Review+1 by ralf_lici

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

socket: Let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 29 insertions(+), 14 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/8

src/openvpn/manage.c +1 -1
index e121b38..46b0abe 100644
@@ -2044,7 +2044,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +16 -8
index 41c8bac..4c0aab5 100644
@@ -627,12 +627,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -678,9 +679,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -914,9 +917,9 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix,
-            bool ipv6only)
+            bool ipv6only, msglvl_t msglevel)
 {
     struct gc_arena gc = gc_new();
 
@@ -940,8 +943,10 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        gc_free(&gc);
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -956,10 +961,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
             print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
     gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 46fa29c..20c10c2 100644
@@ -348,8 +348,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
-                 bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
+                 bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -405,9 +412,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 8
Gerrit-Owner: stipa <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-Reviewer: ralf_lici <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: ralf_lici <[email protected]>
Gerrit-Attention: stipa <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#5 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: plaisthos, ralf_lici, stipa.

Hello plaisthos, ralf_lici,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email

to look at the new patch set (#9).

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

socket: Let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 29 insertions(+), 14 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/9

src/openvpn/manage.c +1 -1
index e121b38..46b0abe 100644
@@ -2044,7 +2044,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +16 -8
index 41c8bac..4c0aab5 100644
@@ -627,12 +627,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -678,9 +679,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -914,9 +917,9 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix,
-            bool ipv6only)
+            bool ipv6only, msglvl_t msglevel)
 {
     struct gc_arena gc = gc_new();
 
@@ -940,8 +943,10 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        gc_free(&gc);
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -956,10 +961,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
             print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
     gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 46fa29c..20c10c2 100644
@@ -348,8 +348,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
-                 bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
+                 bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -405,9 +412,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 9
Gerrit-Owner: stipa <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-Reviewer: ralf_lici <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: ralf_lici <[email protected]>
Gerrit-Attention: stipa <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#6 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Lev Stipakov, Ralf Lici.

Hello Arne Schwabe, Ralf Lici,

I'd like you to reexamine a change. Please visit

    https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email

to look at the new patch set (#11).

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

socket: Let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 29 insertions(+), 14 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/11

src/openvpn/manage.c +1 -1
index 58f63e6..013d516 100644
@@ -2105,7 +2105,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +16 -8
index 307b5c6..988814e 100644
@@ -268,12 +268,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -319,9 +320,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -556,9 +559,9 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
-            const char *prefix, bool ipv6only)
+            const char *prefix, bool ipv6only, msglvl_t msglevel)
 {
     struct gc_arena gc = gc_new();
 
@@ -582,8 +585,10 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        gc_free(&gc);
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -598,10 +603,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
             print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
     gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 52e09c9..88c51fa 100644
@@ -337,8 +337,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
-                 const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+                 const char *prefix, bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -392,9 +399,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 11
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Lev Stipakov <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#7 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Lev Stipakov, Ralf Lici.

Răzvan Cojocaru has posted comments on this change by Lev Stipakov. ( https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

Patch Set 11:

(3 comments)

File src/openvpn/socket.h:

https://gerrit.openvpn.net/c/openvpn/+/1915/comment/bf854817_5c0fe3c1?usp=email :
PS11, Line 343: * non-fatal level and checks the result.
Then if you agree that D_LOW is not great in all cases, maybe drop a line about how log levels change in the function description here.

File src/openvpn/socket.c:

https://gerrit.openvpn.net/c/openvpn/+/1915/comment/6973ad36_b4d4db36?usp=email :
PS11, Line 324: && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
Would M_WARN be better than D_LOW here?

https://gerrit.openvpn.net/c/openvpn/+/1915/comment/feeb54cd_fcb77bf0?usp=email :
PS11, Line 588: msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
msg((msglevel & M_FATAL) ? msglevel : D_LOW,?

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 11
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-CC: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Lev Stipakov <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Comment-Date: Wed, 04 Nov 2026 15:15:56 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#8 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Ralf Lici, Răzvan Cojocaru.

Lev Stipakov has posted comments on this change by Lev Stipakov. ( https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

Patch Set 11:

(4 comments)

File src/openvpn/socket.h:

https://gerrit.openvpn.net/c/openvpn/+/1915/comment/9779ec73_c167a611?usp=email :
PS11, Line 343: * non-fatal level and checks the result.

Quoted 1 line

Then if you agree that D_LOW is not great in all cases, maybe drop a line about how log levels cha […]

Without the conditional above, the function doesn't vary the level — it uses what the caller passes, which the existing doc already says.

File src/openvpn/socket.c:

https://gerrit.openvpn.net/c/openvpn/+/1915/comment/1ba5f4f0_659e43b3?usp=email :
PS7, Line 965: print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));

Quoted 1 line

nit: Should this now print cur->ai_addr rather than local->ai_addr? local may point to an entr […]

This was in pre-existing code, but fixed.

File src/openvpn/socket.c:

https://gerrit.openvpn.net/c/openvpn/+/1915/comment/00884d33_585f7756?usp=email :
PS11, Line 324: && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))

Quoted 1 line

Would M_WARN be better than D_LOW here?

The caller already logs "no usable IPv6 socket, not probing IPv6 remotes" on failure, which has the context this one lacks. I'd rather raise that message in the server-probe patch than warn here and explain it at debug level.

https://gerrit.openvpn.net/c/openvpn/+/1915/comment/815e7277_f75938b2?usp=email :
PS11, Line 588: msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,

Quoted 1 line

msg((msglevel & M_FATAL) ? msglevel : D_LOW,?

I'd rather socket_bind() log at the level it was given than override it internally.

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 11
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-CC: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Răzvan Cojocaru <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Comment-Date: Wed, 04 Nov 2026 15:24:19 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Ralf Lici <[email protected]>
Comment-In-Reply-To: Răzvan Cojocaru <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#9 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Ralf Lici, Răzvan Cojocaru.

Hello Arne Schwabe, Ralf Lici,

I'd like you to reexamine a change. Please visit

    https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email

to look at the new patch set (#12).

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

socket: Let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour.

The failure message now reports the address that was actually bound, cur, rather than the head of the list, local, which may belong to a different address family. Until now that message was always followed by a fatal exit; with an optional bind it is printed while the client carries on, so it needs to name the right address.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 30 insertions(+), 15 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/12

src/openvpn/manage.c +1 -1
index 58f63e6..013d516 100644
@@ -2105,7 +2105,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +17 -9
index 307b5c6..c7de904 100644
@@ -268,12 +268,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -319,9 +320,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -556,9 +559,9 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
-            const char *prefix, bool ipv6only)
+            const char *prefix, bool ipv6only, msglvl_t msglevel)
 {
     struct gc_arena gc = gc_new();
 
@@ -582,8 +585,10 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        gc_free(&gc);
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -598,10 +603,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
-            print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+            print_sockaddr_ex(cur->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
     gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 52e09c9..88c51fa 100644
@@ -337,8 +337,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
-                 const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+                 const char *prefix, bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -392,9 +399,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 12
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-CC: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Răzvan Cojocaru <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#10 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Lev Stipakov, Ralf Lici.

Răzvan Cojocaru has posted comments on this change by Lev Stipakov. ( https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

Patch Set 12: Code-Review+2

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 12
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Lev Stipakov <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Comment-Date: Wed, 04 Nov 2026 15:29:14 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#11 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Lev Stipakov.

Ralf Lici has posted comments on this change by Lev Stipakov. ( https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

Patch Set 12: Code-Review+1

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 12
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Lev Stipakov <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Comment-Date: Wed, 04 Nov 2026 15:31:04 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#12 lore raw

PATCH v12 1/1 socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

From: Lev Stipakov <[email protected]>

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour.

The failure message now reports the address that was actually bound, cur, rather than the head of the list, local, which may belong to a different address family. Until now that message was always followed by a fatal exit; with an optional bind it is printed while the client carries on, so it needs to name the right address.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
This mail reflects revision 12 of this Change.

Acked-by according to Gerrit (reflected above):
Razvan Cojocaru <[email protected]>

src/openvpn/manage.c +1 -1
index 58f63e6..013d516 100644
@@ -2105,7 +2105,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +17 -9
index 307b5c6..c7de904 100644
@@ -268,12 +268,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -319,9 +320,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -556,9 +559,9 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
-            const char *prefix, bool ipv6only)
+            const char *prefix, bool ipv6only, msglvl_t msglevel)
 {
     struct gc_arena gc = gc_new();
 
@@ -582,8 +585,10 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        gc_free(&gc);
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -598,10 +603,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
-            print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+            print_sockaddr_ex(cur->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
     gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 52e09c9..88c51fa 100644
@@ -337,8 +337,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
-                 const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+                 const char *prefix, bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -392,9 +399,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#13 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Lev Stipakov, Ralf Lici, Răzvan Cojocaru.

Hello Arne Schwabe, Ralf Lici, Răzvan Cojocaru,

I'd like you to reexamine a change. Please visit

    https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email

to look at the new patch set (#13).

The following approvals got outdated and were removed:
Code-Review+1 by Ralf Lici, Code-Review+2 by Răzvan Cojocaru

The change is no longer submittable: Code-Review and checks~ChecksSubmitRule are unsatisfied now.

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

socket: Let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour. With more than one return path the gc arena no longer belongs at the top of the function, so it moves into the bind failure, the only branch that formats an address.

The failure message now reports the address that was actually bound, cur, rather than the head of the list, local, which may belong to a different address family. Until now that message was always followed by a fatal exit; with an optional bind it is printed while the client carries on, so it needs to name the right address.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 30 insertions(+), 18 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/13

src/openvpn/manage.c +1 -1
index addd6f2..bbf454a 100644
@@ -2080,7 +2080,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +17 -12
index c07ef60..36cb0e5 100644
@@ -268,12 +268,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -319,9 +320,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -558,12 +561,10 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
-            const char *prefix, bool ipv6only)
+            const char *prefix, bool ipv6only, msglvl_t msglevel)
 {
-    struct gc_arena gc = gc_new();
-
     /* FIXME (schwabe)
      * getaddrinfo for the bind address might return multiple AF_INET/AF_INET6
      * entries for the requested protocol.
@@ -584,8 +585,9 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -600,10 +602,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
-            print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        struct gc_arena gc = gc_new();
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+            print_sockaddr_ex(cur->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
-    gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 2210bfe..084b9cc 100644
@@ -346,8 +346,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
-                 const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+                 const char *prefix, bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -401,9 +408,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 13
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Lev Stipakov <[email protected]>
Gerrit-Attention: Răzvan Cojocaru <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#14 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Attention is currently required from: Arne Schwabe, Lev Stipakov, Ralf Lici.

Răzvan Cojocaru has posted comments on this change by Lev Stipakov. ( https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

Patch Set 13: Code-Review+2

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 13
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Lev Stipakov <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Comment-Date: Tue, 24 Nov 2026 15:48:58 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#15 lore raw

PATCH v13 1/1 socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

From: Lev Stipakov <[email protected]>

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour. With more than one return path the gc arena no longer belongs at the top of the function, so it moves into the bind failure, the only branch that formats an address.

The failure message now reports the address that was actually bound, cur, rather than the head of the list, local, which may belong to a different address family. Until now that message was always followed by a fatal exit; with an optional bind it is printed while the client carries on, so it needs to name the right address.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
This mail reflects revision 13 of this Change.

Acked-by according to Gerrit (reflected above):
Razvan Cojocaru <[email protected]>

src/openvpn/manage.c +1 -1
index addd6f2..bbf454a 100644
@@ -2080,7 +2080,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +17 -12
index c07ef60..36cb0e5 100644
@@ -268,12 +268,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -319,9 +320,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -558,12 +561,10 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
-            const char *prefix, bool ipv6only)
+            const char *prefix, bool ipv6only, msglvl_t msglevel)
 {
-    struct gc_arena gc = gc_new();
-
     /* FIXME (schwabe)
      * getaddrinfo for the bind address might return multiple AF_INET/AF_INET6
      * entries for the requested protocol.
@@ -584,8 +585,9 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -600,10 +602,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
-            print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        struct gc_arena gc = gc_new();
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+            print_sockaddr_ex(cur->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
-    gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 2210bfe..084b9cc 100644
@@ -346,8 +346,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
-                 const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+                 const char *prefix, bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -401,9 +408,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Acked-by Razvan Cojocaru on v13 1/1
#16 lore raw

[Openvpn-devel] [PATCH applied] Re: socket: Let an optional UDP socket give up on a failed bind

This took a while, but now the end result can do what it sets out to do, documentation got improved, and the error message is more correct :-)

Stared long and hard, and then not tested beyond "BB says all green" (the t_server builder exercises the socket code sufficiently).

Your patch has been applied to the master branch.

commit 66ea3e05b6c67cc0438c58140d6ca76be0c315a0

Author: Lev Stipakov
Date:   Tue Nov 24 17:25:50 2026 +0100

socket: Let an optional UDP socket give up on a failed bind

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
Message-Id: <[email protected]>
URL: https://lore.kernel.org/openvpn-devel/[email protected]

--
kind regards,

Gert Doering

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#17 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Gert Doering has uploaded a new patch set (#14) to the change originally created by Lev Stipakov. ( https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by Răzvan Cojocaru

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

socket: Let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour. With more than one return path the gc arena no longer belongs at the top of the function, so it moves into the bind failure, the only branch that formats an address.

The failure message now reports the address that was actually bound, cur, rather than the head of the list, local, which may belong to a different address family. Until now that message was always followed by a fatal exit; with an optional bind it is printed while the client carries on, so it needs to name the right address.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
Message-Id: <[email protected]>
URL: https://lore.kernel.org/openvpn-devel/[email protected]

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 30 insertions(+), 18 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/15/1915/14

src/openvpn/manage.c +1 -1
index addd6f2..bbf454a 100644
@@ -2080,7 +2080,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +17 -12
index c07ef60..36cb0e5 100644
@@ -268,12 +268,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -319,9 +320,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -558,12 +561,10 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
-            const char *prefix, bool ipv6only)
+            const char *prefix, bool ipv6only, msglvl_t msglevel)
 {
-    struct gc_arena gc = gc_new();
-
     /* FIXME (schwabe)
      * getaddrinfo for the bind address might return multiple AF_INET/AF_INET6
      * entries for the requested protocol.
@@ -584,8 +585,9 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -600,10 +602,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
-            print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        struct gc_arena gc = gc_new();
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+            print_sockaddr_ex(cur->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
-    gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 2210bfe..084b9cc 100644
@@ -346,8 +346,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
-                 const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+                 const char *prefix, bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -401,9 +408,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 14
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#18 lore raw

[Openvpn-devel] [S] Change in openvpn[master]: socket: Let an optional UDP socket give up on a failed bind

In reply to a message not in the archive

Gert Doering has submitted this change. ( https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email )

Change subject: socket: Let an optional UDP socket give up on a failed bind
......................................................................

socket: Let an optional UDP socket give up on a failed bind

create_socket_udp_configured()'s "optional" argument covers only the socket() call: socket_bind() is fatal, so a caller that can do without the socket still dies if the address is unavailable.

That is a problem for the upcoming --server-probe, which binds a probe socket for every address family in use, while a connection binds only the family of the remote it is about to try. A --lport whose IPv6 port happens to be taken by another process would therefore kill a client that could have connected over IPv4.

Give socket_bind() a msglevel and a return value, and let "optional" apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is returned, so the caller can skip that address family. Every existing caller passes M_FATAL and keeps its behaviour. With more than one return path the gc arena no longer belongs at the top of the function, so it moves into the bind failure, the only branch that formats an address.

The failure message now reports the address that was actually bound, cur, rather than the head of the list, local, which may belong to a different address family. Until now that message was always followed by a fatal exit; with an optional bind it is printed while the client carries on, so it needs to name the right address.

Change-Id I24e5925a72aaede53ef9e863fe96fcd7363df402

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
Message-Id: <[email protected]>
URL: https://lore.kernel.org/openvpn-devel/[email protected]

---
M src/openvpn/manage.c
M src/openvpn/socket.c
M src/openvpn/socket.h

3 files changed, 30 insertions(+), 18 deletions(-)
src/openvpn/manage.c +1 -1
index addd6f2..bbf454a 100644
@@ -2080,7 +2080,7 @@
         {
             man->connection.sd_top = create_socket_tcp(man->settings.local);
             socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
-                        "MANAGEMENT", false);
+                        "MANAGEMENT", false, M_FATAL);
         }
 
         /*
src/openvpn/socket.c +17 -12
index c07ef60..36cb0e5 100644
@@ -268,12 +268,13 @@
     {
         if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
         {
-            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+            socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+                        M_FATAL);
         }
         else
         {
             socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
-                        sock->info.bind_ipv6_only);
+                        sock->info.bind_ipv6_only, M_FATAL);
         }
     }
 }
@@ -319,9 +320,11 @@
     }
 
     socket_apply_options(sd, sbs, mark, bind_dev);
-    if (bind_addr)
+    if (bind_addr
+        && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
     {
-        socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+        openvpn_close_socket(sd);
+        return SOCKET_UNDEFINED;
     }
     return sd;
 }
@@ -558,12 +561,10 @@
     return new_sd;
 }
 
-void
+bool
 socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
-            const char *prefix, bool ipv6only)
+            const char *prefix, bool ipv6only, msglvl_t msglevel)
 {
-    struct gc_arena gc = gc_new();
-
     /* FIXME (schwabe)
      * getaddrinfo for the bind address might return multiple AF_INET/AF_INET6
      * entries for the requested protocol.
@@ -584,8 +585,9 @@
     }
     if (!cur)
     {
-        msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+        msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
             addr_family_name(ai_family));
+        return false;
     }
 
     if (ai_family == AF_INET6)
@@ -600,10 +602,13 @@
     }
     if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
     {
-        msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
-            print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+        struct gc_arena gc = gc_new();
+        msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+            print_sockaddr_ex(cur->ai_addr, ":", PS_SHOW_PORT, &gc));
+        gc_free(&gc);
+        return false;
     }
-    gc_free(&gc);
+    return true;
 }
 
 int
src/openvpn/socket.h +12 -5
index 2210bfe..084b9cc 100644
@@ -346,8 +346,15 @@
 
 struct link_socket *link_socket_new(void);
 
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
-                 const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+                 const char *prefix, bool ipv6only, msglvl_t msglevel);
 
 int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
                     volatile int *signal_received);
@@ -401,9 +408,9 @@
  * Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
  * --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
  * per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
  */
 socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
                                                  const struct socket_buffer_size *sbs, int mark,

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1915?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Gerrit-Change-Number: 1915
Gerrit-PatchSet: 14
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-Reviewer: Răzvan Cojocaru <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel